package com.gee.spring.cloud.data.transform.core.center.config.handler;

import com.gee.spring.cloud.common.core.api.result.ApiResult;
import com.gee.spring.cloud.common.core.entity.center.config.OrgCenterMapping;
import com.gee.spring.cloud.common.starter.config.event.CenterConfigEventHandler;
import com.gee.spring.cloud.common.starter.feign.center.config.OrgCenterMappingClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;
import java.util.StringJoiner;
import java.util.concurrent.ConcurrentHashMap;

import static cn.hutool.core.text.StrPool.UNDERLINE;

/**
 * @author gepengjun
 * @since 2024/3/19 14:31
 */
@Component
public class OrgCenterMappingHandler extends CenterConfigEventHandler<OrgCenterMapping> {

    //key: orgId_mappingOrgCenterId
    private final ConcurrentHashMap<String, OrgCenterMapping> orgCenterMappingMap = new ConcurrentHashMap<>();

    @Autowired
    private OrgCenterMappingClient orgCenterMappingClient;

    public OrgCenterMapping getByOrgIdAndOrgCenterId(Long orgId, Long orgCenterId) {
        String key = buildKey(orgId, orgCenterId);
        return orgCenterMappingMap.computeIfAbsent(key, newKey -> orgCenterMappingClient.getByOrgIddAndOrgCenterId(orgId, orgCenterId).getData());
    }

    public OrgCenterMapping getByOrgIdAndMappingCenterIdentity(Long orgId, Long mappingCenterIdentity) {
        Optional<OrgCenterMapping> orgCenterMappingOptional = orgCenterMappingMap.values().stream()
                .filter(orgCenterMapping -> orgCenterMapping.getMappingCenterIdentity() == mappingCenterIdentity)
                .findFirst();
        if (orgCenterMappingOptional.isPresent()){
            return orgCenterMappingOptional.get();
        }else {
            ApiResult<OrgCenterMapping> orgCenterMappingApiResult = orgCenterMappingClient.getByOrgIdAndMappingCenterIdentity(orgId, mappingCenterIdentity);
            OrgCenterMapping orgCenterMapping = orgCenterMappingApiResult.getData();
            if (orgCenterMappingApiResult.isSuccess() && orgCenterMapping != null){
                String key = buildKey(orgCenterMapping.getOrgId(), orgCenterMapping.getOrgCenterId());
                orgCenterMappingMap.put(key, orgCenterMapping);
                return orgCenterMapping;
            }else {
                return null;
            }
        }
    }

    @Override
    public void handlerUpdate(List<OrgCenterMapping> dataList, List<OrgCenterMapping> oldList) {
        dataList.forEach(orgCenterMapping ->{
            String key = buildKey(orgCenterMapping.getOrgId(), orgCenterMapping.getOrgCenterId());
            orgCenterMappingMap.computeIfPresent(key, (oldKey, old) -> orgCenterMapping);
        });
    }

    @Override
    public void handlerDelete(List<OrgCenterMapping> oldList) {
        oldList.forEach(orgCenterMapping -> {
            String key = buildKey(orgCenterMapping.getOrgId(), orgCenterMapping.getOrgCenterId());
            orgCenterMappingMap.remove(key);
        });
    }

    private static String buildKey(Long orgId, Long mappingDevId){
        return new StringJoiner(UNDERLINE).add(orgId.toString()).add(mappingDevId.toString()).toString();
    }
}
